//
//  MEViewController.m
//  Holle
//
//  Created by 夏传海 on 2016/12/5.
//  Copyright © 2016年 夏传海. All rights reserved.
//

#import "MEViewController.h"
#import "AppDelegate.h"

@interface MEViewController ()

@property (nonatomic,strong) UISegmentedControl *segmentControl;

@end

@implementation MEViewController
{
    UIView *_animationView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self startSegment];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我的账户";
    self.view.backgroundColor = [UIColor blackColor];
    
    self.navigationItem.hidesBackButton = YES;   //隐藏返回按钮
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 30, 20);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];

}

- (void) openOrCloseLeftList
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.LeftSlideVC.closed)
    {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"viewWillDisappear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear");
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)segmentAction:(UISegmentedControl *)control
{
    CGFloat x = self.view.frame.size.width / 2 * control.selectedSegmentIndex;
    [UIView animateWithDuration:0.2 animations:^{
        _animationView.frame = CGRectMake(x, 108, self.view.frame.size.width / 2, 5);
    }];
}

#pragma mark 设置Segment空间

- (void)startSegment
{
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"钻石",@"金币",]];
    [self.segmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.segmentControl.frame = CGRectMake(0, 64, self.view.frame.size.width, 44);
    _segmentControl.backgroundColor = [UIColor blackColor];
    
    _animationView = [[UIView alloc] init];
    _animationView.frame = CGRectMake(0, 108, self.view.frame.size.width / 2, 5);
    _animationView.backgroundColor = [UIColor colorWithRed:126.0/225 green:87.0/225 blue:194.0/225 alpha:1];
    
    [self.view addSubview:_animationView];
    [self.view addSubview:self.segmentControl];
    
    //分段控制器颜色
    
    _segmentControl.tintColor=[UIColor clearColor];
    _segmentControl.selectedSegmentIndex = 0;
    
    
    NSDictionary *selected = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
                               NSForegroundColorAttributeName:[UIColor colorWithRed:126.0/225 green:87.0/225 blue:194.0/225 alpha:1]};
    NSDictionary *normal = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
                             NSForegroundColorAttributeName:[UIColor whiteColor]};
    
    [_segmentControl setTitleTextAttributes:normal forState:UIControlStateNormal];
    [_segmentControl setTitleTextAttributes:selected forState:UIControlStateSelected];
    [_segmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_segmentControl];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


    @end

